#include "CVarSystem.h"
#include "../infLib/infLib.h"

CVar*   CVar::staticLink = NULL;



CVarSystem   cvarSystemLocal;
CVarSystem* cvarSystem = &cvarSystemLocal;

CVarSystem::CVarSystem()
{
    if( CVar::staticLink )
        cvarsList.add_unique( CVar::staticLink );
}

CVarSystem::~CVarSystem() {
}

void CVarSystem::Init()
{
    if( CVar::staticLink )
        cvarsList.add_unique( CVar::staticLink );

    // add internal cvar...

    
}

void CVarSystem::Shutdown()
{
	Clear();
}

void CVarSystem::Register( CVar* cvar )
{

}

void CVarSystem::LinkCVars( CVar* cvars )
{
    CVar* logCvar = cvars;
    while( logCvar ) {
        inf::printf( "CVar\t%s\t= %s", logCvar->GetName(), logCvar->GetString() );

        logCvar = logCvar->next;
    }

    if( cvars )
        cvarsList.add_unique( cvars );
}

void    CVarSystem::Clear() {
	cvarsList.clear();
}

bool CVarSystem::ResetCVar( const char* name )
{
	CVar* found = FindCVar( name );
	if( found ) {
		found->ResetToDefault();
	}
	return found != NULL;
}

void CVarSystem::ResetFlaggedVariables( cvarFlags_t flags, cvarFlags_t ignoreFlags /*= 0 */ )
{
	// ...
}

bool CVarSystem::CheckExist( const char* name ) const
{
	return FindCVar( name ) != NULL;
}

CVar* CVarSystem::FindCVar( const char* name ) const
{
	CVar* cvar = NULL;
	for( int i=0; i<cvarsList.size(); ++i ) {
	    cvar = cvarsList[i];    //CVar::GetStaticLink();
	    while( cvar ) {
		    if( inf::Str::cmp(cvar->name, name, false) == 0 ) {
			    // found!
			    return cvar;
		    }
		    cvar = cvar->next;
	    }
	}

    inf::printf( "Not exist CVar: %s\n", name );
	return cvar;    // null
}

void CVarSystem::SetString( const char* name, const char* value, cvarFlags_t flags )
{
	CVar* found = FindCVar( name );
	if( found ) {
		found->SetString( value );
	}
}

const char* CVarSystem::GetString( const char* name ) const
{
	CVar* found = FindCVar( name );
	if( found ) {
		return found->GetString();
	}
	return NULL;
}

void CVarSystem::SetBool( const char* name, bool b, cvarFlags_t flags )
{
	CVar* found = FindCVar( name );
	if( found ) {
		found->SetBool(b);
	}
}

bool CVarSystem::GetBool( const char* name ) const
{
	CVar* found = FindCVar( name );
	if( found ) {
		return found->GetBool();
	}
	return false;
}

void CVarSystem::SetInteger( const char* name, int i, cvarFlags_t flags )
{
	CVar* found = FindCVar( name );
	if( found ) {
		found->SetInteger(i);
	}
}

int CVarSystem::GetInteger( const char* name ) const
{
	CVar* found = FindCVar( name );
	if( found ) {
		return found->GetInteger();
	}
    return inf::math::LIMITS_INTEGER_MAX;
}

void CVarSystem::SetFloat( const char* name, float f, int flags)
{
	CVar* found = FindCVar( name );
	if( found ) {
		found->SetFloat(f);
	}
}

float CVarSystem::GetFloat( const char* name ) const
{
	CVar* found = FindCVar( name );
	if( found ) {
		return found->GetFloat();
	}
    return inf::math::LIMITS_FLOAT_MAX;
}

